<template>
	<view class="classlist">
		<view class="loadingLayout" v-if="!classList.length && totalCount">
			<uni-load-more status="loading"></uni-load-more>
		</view>
		<view class="content">
			<navigator :url="'/pages/preview/preview?id='+item._id" class="item" v-for="item in classList" :key="item._id">
				<image :src="item.smallPicurl" mode="aspectFill" :lazy-load="true"></image>
			</navigator>
		</view>
		<view class="loadingLayout"  v-if="classList.length || !totalCount">
			<uni-load-more :status="loadStatus"></uni-load-more>
		</view>
		<view class="safe-area-inset-bottom"></view>
	</view>
</template>

<script setup>
	import {ref} from 'vue';
	import {onLoad,onUnload,onReachBottom,onShareAppMessage,onShareTimeline} from '@dcloudio/uni-app';
	import {apiGetWallList,apiGetHistoryList} from  '@/api/apis.js';
	import {gotoHome} from '@/utils/common.js'
	const classList = ref([]);
	const queryParams = {
		pageNum:1
	};
	const totalCount = ref(9);
	const loadStatus = ref('more');
	let pageName;
	
	//获取分类列表 加载数据的时候要判断一开始来页面的时候有没有数据，首次加载loading触底的loading
	const getClassList = async function(){
		let res;
		loadStatus.value = 'loading';
		if(queryParams.classid) res = await apiGetWallList(queryParams);
		if(queryParams.type) res = await apiGetHistoryList(queryParams);
		console.log(res)
		if(res.total) {
			totalCount.value = res.total;
			if(res.total==0) {
				loadStatus.value = 'nomore';
				return;
			}
			queryParams.pageNum++;
		}
		loadStatus.value = 'more';
		classList.value.push(...res.data);
		uni.setStorageSync("storageClassList",classList.value);
	}
	
	onLoad(({id,name,type})=>{
		if(type) queryParams.type = type;
		if(id) queryParams.classid = id;
		// if(!_id) gotoHome();
		pageName = name;
		getClassList();
		uni.setNavigationBarTitle({
			title:name
		});
	})
	
	onUnload(()=>{
		uni.removeStorageSync("storageClassList");
	})
	
	//触底加载
	onReachBottom(()=>{
		if(classList.value.length>=totalCount.value||totalCount.value==0){ loadStatus.value = 'no-more'; return;}
		getClassList();
	})
	
	onShareAppMessage((e)=>{
		return{
			title:`壁壁生笙，-${pageName}`,
			path:`/pages/classlist/classlist?id=${queryParams.classid}&name=${pageName}`
		}
	})
	
	//分享朋友圈 是参数 不是全部的路径
	onShareTimeline((e)=>{
		return{
			title:`壁壁生笙，-${pageName}`,
			query:`id=${queryParams.classid}&name=${pageName}`
		}
	})
</script>

<style lang="scss" scoped>
	.classlist{
		padding: 0 5rpx;
		.content{
			display: grid;
			gap: 5rpx;
			grid-template-columns: repeat(3,1fr);
			grid-template-rows: repeat(3,1fr);
			.item{
				height: 340rpx;
				// width: 150rpx;
				image{
					width: 100%;
					height: 100%;
					display: block; //细节   有时间隔不一样
				}
			}
		}
	}
</style>
